# 爬取玩家列表页,生成csv文件
# 有个bug，无法爬取自己账号的数据

import time
import re
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
import csv
from datetime import datetime

# 定义 URL
game_login_url = "https://ts1.x1.asia.travian.com/"
base_count_list_url = "https://ts1.x1.asia.travian.com/statistics/player/overview?page="

# 配置浏览器选项
options = Options()
driver = webdriver.Chrome(options=options)
driver.set_window_size(1200, 900)

# 登录
driver.get(game_login_url)
time.sleep(3)  # 等待页面加载

username_field = driver.find_element(By.NAME, "name")
username_field.send_keys("小蜜蜂#bee")  # 替换为你的用户名
time.sleep(1)

password_field = driver.find_element(By.NAME, "password")
password_field.send_keys("87654321")  # 替换为你的密码
time.sleep(1)

# 点击登录按钮
login_button = driver.find_element(By.CSS_SELECTOR, "button[type='submit']")
login_button.click()
time.sleep(3)  # 等待登录完成

# 获取当前日期
current_date = datetime.now().strftime("%Y%m%d")
print(driver.current_url)

# 创建CSV文件
csv_filename = f"{current_date}.csv"
try:
    with open(csv_filename, mode="w", newline="", encoding="utf-8") as file:
        writer = csv.writer(file)
        # 写入表头
        writer.writerow(["玩家", "统计时间", "玩家排名", "人口", "村莊"])

        page = 1
        while True:
            # 导航到目标页面
            count_list_url = f"{base_count_list_url}{page}"
            driver.get(count_list_url)
            time.sleep(2)  # 等待页面加载

            # 检查页面是否有表格数据
            try:
                WebDriverWait(driver, 3).until(
                    EC.presence_of_element_located((By.XPATH, "//tbody[@class='hoverable']"))
                )
            except:
                print(f"第 {page} 页没有找到表格数据，停止遍历。")
                break

            # 提取数据
            rows = driver.find_elements(By.XPATH, "//tbody[@class='hoverable']/tr[@class='hover']")
            for row in rows:
                try:
                    rank = re.sub(r'\u202e|\u202c', '', row.find_element(By.XPATH, ".//td[1]").text.strip())
                    player_name = re.sub(r'\u202e|\u202c', '', row.find_element(By.XPATH, ".//td[3]/a").text.strip())
                    population = re.sub(r'\u202e|\u202c', '', row.find_element(By.XPATH, ".//td[6]").text.strip().replace(",", ""))
                    villages = re.sub(r'\u202e|\u202c', '', row.find_element(By.XPATH, ".//td[7]").text.strip())

                    # 写入CSV
                    writer.writerow([player_name, current_date, rank, population, villages])
                except Exception as e:
                    print(f"某些元素未找到：{e}")

            # 检查是否存在下一页按钮且不可点击
            try:
                next_button = WebDriverWait(driver, 3).until(
                    EC.presence_of_element_located((By.XPATH, "//img[@class='next disabled']"))
                )
                if next_button:
                    print(f"到达最后一页：第 {page} 页")
                    break
            except:
                pass

            page += 1  # 准备获取下一页的数据

except Exception as e:
    print(f"写入CSV文件时出错：{e}")

print(f"数据已成功保存到文件：{csv_filename}")

# 关闭浏览器
driver.quit()